package com.sumainfor.wx.tenant.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sumainfor.common.Constants;
import com.sumainfor.common.util.ContextUtils;
import com.sumainfor.common.utlis.ComUtils;
import com.sumainfor.common.utlis.ParamsCommon;
import com.sumainfor.wx.tenant.dao.TctenTenantMiddleMapper;
import com.sumainfor.wx.tenant.dao.TctenTenantSchoolMapper;
import com.sumainfor.wx.tenant.dao.UtilsMapper;
import com.sumainfor.wx.tenant.entity.TctenTenantSchool;
import com.sumainfor.wx.tenant.service.TctenTenantMiddleService;
import com.sumainfor.wx.tenant.service.TctenTenantSchoolService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 *租客注册和创建工单的中间表
 *
 */
@Service
public class TctenTenantMiddleServiceImpl implements TctenTenantMiddleService, Serializable {
    private ReentrantLock lock = new ReentrantLock(false);

    Logger log = LoggerFactory.getLogger(TctenTenantMiddleServiceImpl.class);// 日志
    @Autowired
    private TctenTenantMiddleMapper tctenTenantMiddleMapper;
    @Autowired
    private UtilsMapper utilsMapper;
    String UUID=null;
    /**
     * 根据手机号查询中间表是否存在一条记录
     */
    @Override
    public Map<String, Object> getTctenTenantMiddle(Map<String, Object> params) {
        return tctenTenantMiddleMapper.getTctenTenantMiddle(params);
    }
    /**
     * 新增一条记录
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer addTctenTenantMiddle(Map<String, Object> params) {
        Integer result=0;
        /**
         * 加锁操作
         */
        lock.lock();
        try{
            try {

                params.put("term", "1");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
                params.put("fnc",
                        this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
                ParamsCommon.commonInsertParms(params);
                tctenTenantMiddleMapper.addTctenTenantMiddle(params);
            } catch (Exception e) {
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        }
        finally {
            lock.unlock();
        }
        return result;
    }
    /**
     * 市场部根据手机号查询是否有记录存在如果就有 带着记录返回 ，如果没有就新增
     */

    public Map<String, Object> getTctenForTcmkt(Map<String, Object> params) {
        /**
         * 加锁操作
         */
        lock.lock();
        try{
            try {
                Map<String,Object> map=tctenTenantMiddleMapper.getTctenTenantMiddle(params);
                System.out.println("map:"+map);
                if (map!=null && map.size()>0){
                    params=map;
                }else{
//                    tenantMap.put("by", ContextUtils.getUserId());//操作人
                    params.put("term", "1");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
                    params.put("fnc",
                            this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
                    ParamsCommon.commonInsertParms(params);
                    //租客编号
                    Map<String,Object>uuid=utilsMapper.getUUID(new HashMap<String,Object>(){{
                        put("uuid", "SysId");
                        put("database", Constants.DATA_SOURCE_TCTEN);
                        put("surface", "tcten_tenant_middle");
                    }});
                    if(uuid!=null &&(!uuid.get("uuid").toString().equals(""))){
                        int number=Integer.valueOf(uuid.get("uuid").toString());
                        UUID= ComUtils.getLocalTrmSeqNum("ti",number);
                    }else{
                        UUID=ComUtils.getLocalTrmSeqNum("ti",0);
                    }
                    params.put("SysId",UUID);
                    tctenTenantMiddleMapper.addTctenTenantMiddle(params);
                }

            } catch (Exception e) {
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        }
        finally {
            lock.unlock();
        }
        return  params;
    }
}
